package com.xuegao.xuegaospringboottest.controller;

import com.xuegao.xuegaospringboottest.utils.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class RedisController {
    private static final Logger log = LoggerFactory.getLogger(RedisController.class);

    public static final String REDIS_KEY = "test_key";

    @RequestMapping("/test/setStr")
    public String setStr() {
        RedisUtil.set(REDIS_KEY, "set Str");
        return "succcess";
    }

    @RequestMapping("/test/getStr")
    public String getStr() {
        Object o = RedisUtil.get(REDIS_KEY);
        log.info("[xue-gao-spring-boot-test][RedisController][getStr][o={}]", o);
        return o.toString();
    }

    @RequestMapping("/test/del")
    public String del() {
        RedisUtil.del(REDIS_KEY);
        return "succcess";
    }

    @RequestMapping("/test/expire")
    public String expire() {
        RedisUtil.expire(REDIS_KEY, 10, TimeUnit.SECONDS);
        return "succcess";
    }

    @RequestMapping("/test/lock")
    public Boolean lock() {
        boolean lock = RedisUtil.lock(REDIS_KEY, 10, "diy");
        log.info("[xue-gao-spring-boot-test][RedisController][lock][lock={}]", lock);
        return lock;
    }

    @RequestMapping("/test/lockV2")
    public Boolean lockV2() {
        boolean lockV2 = RedisUtil.lockV2(REDIS_KEY, 10, "1");
        log.info("[xue-gao-spring-boot-test][RedisController][lockV2][lockV2={}]", lockV2);
        return lockV2;
    }

    @RequestMapping("/test/unLock")
    public Boolean unLock() {
        boolean unLock = RedisUtil.unLock(REDIS_KEY, "1");
        log.info("[xue-gao-spring-boot-test][RedisController][unLock][unLock={}]", unLock);
        return unLock;
    }
}
